package org.ecype.diego;

import java.lang.reflect.Field;
import java.util.Arrays;

import androidx.persistence.CascadeType;
import androidx.persistence.ManyToOne;
import androidx.persistence.OneToOne;

public class Reference {

	private final Entity parent;
	private final Entity child;

	private String linkToChild;
	private String linkToParent;
	private boolean cascadeRemove;
	private boolean optinal;
	private final Field field;

	public Reference(Entity parent, Entity child, Field field) {
		this.parent = parent;
		this.child = child;
		this.field = field;
	}

	public Field getField() {
		return field;
	}

	public Entity getParent() {
		return parent;
	}

	public void setLinkToParent(String linkToParent) {
		this.linkToParent = linkToParent;
	}

	public String getLinkToParent() {
		return linkToParent;
	}

	public String getParentColumn() {
		return parent.getIdColumn().getName();
	}

	public Entity getChild() {
		return child;
	}

	public void setLinkToChild(String linkToChild) {
		this.linkToChild = linkToChild;
	}

	public String getLinkToChild() {
		return linkToChild;
	}

	public boolean isCascadeRemove() {
		return cascadeRemove;
	}

	public boolean isOptinal() {
		return optinal;
	}

	public void setup(ManyToOne annotation) {
		cascadeRemove = Arrays.asList(annotation.cascade()).contains(CascadeType.REMOVE);
		optinal = annotation.optional();
	}

	public void setup(OneToOne annotation) {
		cascadeRemove = Arrays.asList(annotation.cascade()).contains(CascadeType.REMOVE);
		optinal = annotation.optional();
	}

}
